﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UCore;

namespace Twin {
    public partial class frmRdArea : Form {

        public string GeneratedString { get; private set; }
        private string[] arg;
        //private string args;

        private int INST_BITS = 0;
        private int INST_OP = 1;
        private int INST_CMFG = 2;
        private int INST_VM_AREA = 3;

        private int iChip;
        private XPort xport = new XPort();

        public frmRdArea(XPort xport, string content, int iChip) {
            InitializeComponent();
            this.iChip = iChip;
            this.xport = xport;

            GeneratedString = content;
            
            arg = content.Split(',');
            //
            arg[INST_VM_AREA] = UI.init_cmbVmArea(cmbVM, cmbArea, arg[INST_VM_AREA]);
            //UI.init_cmbCom(cmbCom);

            arg[INST_CMFG] = UI.init_cmbCmfg(cmbCmfg, arg[INST_CMFG]);
            //UI.init_cmbCom(cmbCom, Ini.defaultPort);
        }


        private void frmRdArea_Load(object sender, EventArgs e) {
            //init_cmbVmArea();
            //init_cmbCmfg();
            //this.TopMost = true;
            this.FormBorderStyle = FormBorderStyle.FixedSingle; // 设置窗体边框样式为固定单线框
            this.MaximizeBox = false;   // 移除最大化按钮
            this.MinimizeBox = false;   // 禁止调整大小

            //显示芯片型号
            lblChip.Text += Ini.chip[iChip].name;

            TConsole.PositionFormAtTopRight(this);
        }


        private string getVM(string v_m) {
            switch (v_m) {
                case "v":
                    return "0";
                    //break;

                case "m":
                    return "1";
                    //break;
            }
            return "";
        }

        
        private void btnGenerate_Click(object sender, EventArgs e) {
            //OP_RD_AREA, [cmfg], 00[area_name]
            GeneratedString = arg[INST_OP] + "," + arg[INST_CMFG] + "[cmfg]," + arg[INST_VM_AREA] + "[vm_area]";
            this.Close();
        }


        private void cmbVM_SelectedIndexChanged(object sender, EventArgs e) {
            arg[INST_VM_AREA] = cmbVM.SelectedIndex.ToString() + cmbArea.SelectedIndex.ToString();
        }


        private void cmbArea_SelectedIndexChanged(object sender, EventArgs e) {
            arg[INST_VM_AREA] = cmbVM.SelectedIndex.ToString() + cmbArea.SelectedIndex.ToString();
        }


        private void cmbCmfg_SelectedIndexChanged(object sender, EventArgs e) {
            arg[INST_CMFG] = cmbCmfg.SelectedItem.ToString();
            if (arg[INST_CMFG].Equals("空")) {
                arg[INST_CMFG] = "";
            }
        }


        private void btnSend_Click(object sender, EventArgs e) {
            TConsole.txtSnMsg.Text = TConsole.SnMsg.ToString();
            
            //int iPort = int.Parse(cmbCom.Text.Replace("com", "")) - 1;
            CInfo cInfo = new CInfo();

            Msg.setInfo_bits(ref cInfo, arg[INST_BITS]);
            cInfo.sOpArgs = arg[INST_OP] + "," + arg[INST_CMFG] + "," + arg[INST_VM_AREA];
            string wr_kvs = "";

            TConsole.sendAsProtocol(xport, ref cInfo, wr_kvs, "cobra");
        }


        private void label3_Click(object sender, EventArgs e) {

        }


        private void textBox2_TextChanged(object sender, EventArgs e) {

        }


        private void textBox3_TextChanged(object sender, EventArgs e) {

        }


        private void textBox1_TextChanged(object sender, EventArgs e) {

        }
    }

}

